#include "../include/threadPool.h"
int main(int argc, char *argv[])
{
    threadPool_t threadPool;
    int workerNum = atoi(argv[3]);
    threadPoolInit(&threadPool,workerNum);
    makeWorker(&threadPool);
    int sockfd = tcpInit(argv[1],argv[2]);
    printf("正在listen\n");
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd);
    struct epoll_event readyset[1024];
	
	char *fdtoname[1024];
	bzero(&fdtoname,sizeof(fdtoname));
    while(1){
        int readynum = epoll_wait(epfd,readyset,1024,-1);
        for(int i = 0; i < readynum; ++i){
            if(readyset[i].data.fd == sockfd){
				//有人连上来了，先接收并加入监听，问他账号密码
				int netfd = accept(sockfd,NULL,NULL);
                printf("main finish accept,netfd = %d\n",netfd);
				epollAdd(epfd,netfd);
				char buf1[] = "请输入用户名!\n";
				sendCommand(netfd,CMD_USER,buf1,sizeof(buf1));
            }
			else {
				char buf[256];
				Command cmd;
                int buf_len;
				bzero(buf,sizeof(buf));
				int curfd = readyset[i].data.fd;
				int ret = recvCommand(curfd,&cmd,buf,&buf_len);
                printf("main get netfd,curfd = %d\n,cmd = %d,buf = %s\n",curfd,cmd,buf);
				ERROR_CHECK(ret,-1,"recv_respond");
				//有人发消息了，先判断消息的类型，
				//如果是账号密码，就登录验证
			    //如果是目录命令，就先判断这个人是否登录
				//但是我想把它丢给工人，让工人去判断
				pthread_mutex_lock(&threadPool.mutex);
				enQueue(&threadPool.taskQueue,curfd,cmd,buf);
				pthread_cond_broadcast(&threadPool.cond);
				pthread_mutex_unlock(&threadPool.mutex);
			}
        }
    }
    return 0;
}


